package aufgabe1;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author benjamin
 */
public class TreeForm extends JFrame {
    private Tree.Node _root;
   
    /**
     * Represent a tree node
     */
    private class Node extends JPanel {
        private static final int HEIGHT = 40; // Circle size
        private static final int WIDTH = 40;  // Circle size
        private Tree.Node _node;
        
        /**
         * Constructor
         * Link tree node with this node
         * @param node 
         */
        public Node(Tree.Node node) {
            _node = node;
        }
        
        @Override
        public void paint(Graphics g) {
            /*
             * Paint super class
             */
            super.paint(g);
            
            /*
             * Paint circle
             */
            int y = HEIGHT / 2;
            int x = getWidth() / 2;
            g.fillOval(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
            
            /*
             * Paint lines (only if tree node isn't a leaf) 
             */
            if (!_node.isLeaf()) {
                int count = _node.maxChildrens();
                int segSize = getWidth() / count;
                int mid = segSize / 2;
                for (int i = 0; i < count; i++) {
                    int x1 = i * segSize + mid;
                    g.drawLine(x1, getHeight(), x, y);
                }
            }
            
            /*
             * Paint the value of the tree node
             */
            g.setColor(Color.RED);
            g.drawString(_node.getValue().toString(), x-5, y);
            
        }
    }
    
    /**
     * Constructor
     * @param root - root tree node
     * @param t 
     */
    public TreeForm(Tree.Node root, String title) {
        _root = root;
        /*
         * init form
         */
        init(title);
    }
    
    /*
     * init main graphical components
     */
    private void init(String title) {
        setTitle(title);
        setLayout(new GridLayout(0, 1));
        
        Node nRoot = new Node(_root);
        add(nRoot);
               
        /*
         * build and paint tree
         */
        traverse();
        
        
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);

    }
    
    
    /**
     * build and paint tree
     */
    private void traverse() {
        int parentsCount = 1; // root is already set
        int maxChilds = _root.maxChildrens(); // maximal number of childs per tree node
        Tree.Node[] parents = new Tree.Node[parentsCount];
        Tree.Node[] childs = new Tree.Node[parentsCount * maxChilds];
        parents[0] = _root; // init value
        int childCount = 0;
        boolean haveParents = true;
        int level = 0;
        
        
        while (haveParents && (level < 7)) {
            haveParents = false;
            /*
             * For each level a new JPanel
             */
            JPanel p = new JPanel(new GridLayout(1, 0));
            add(p);
            /*
             * Level by level traversion
             */
            level++;
            
            for (int i = 0; i < parentsCount; i++) {
                for (int j = 0; j < maxChilds; j++) {
                    if (parents[i] != null) {
                        childs[childCount] = parents[i].getChild(j);
                        if (childs[childCount] != null) {
                            p.add(new Node(childs[childCount]));
                            if (!childs[childCount].isLeaf()) {
                                haveParents = true;
                            }
                        } else {
                            p.add(new JLabel());
                        }
                    } else {
                        childs[childCount] = null;
                        p.add(new JLabel());
                    }
                    childCount++;
                }
                //
            }
            parents = childs; // childs get parents in next loop
            parentsCount = childCount; // set counter for loop
            childs = new Tree.Node[childCount * maxChilds];
            childCount = 0;
            
        }   
    }
    
    public void update(Tree.Node root, String title) {
        getContentPane().removeAll();
        repaint();
        setVisible(false);
        setLayout(new GridLayout(0, 1));
        _root = root;
        Node nRoot = new Node(_root);
        add(nRoot);
        setTitle(title);
        traverse();
        setVisible(true);
    }
}
